package com.example.todolist.viewmodel

import android.app.Application
import android.content.Context
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.todolist.commen.TodoCategoryEnum
import com.example.todolist.database.model.CategoryEntity
import com.example.todolist.entity.CategoryDetail
import com.example.todolist.entity.TodoCategory
import com.example.todolist.repository.CategoryRepository
import com.example.todolist.util.LoggerTag
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.collections.mutableListOf

@HiltViewModel
class CategoryDetailViewModel @Inject constructor(
    private val repository: CategoryRepository
) : ViewModel() {
    //    private val repository: CategoryRepository = CategoryRepository.getInstance(application) // 单例
    private val _cDatails = MutableLiveData<List<CategoryDetail>>()
    val cDetails: LiveData<List<CategoryDetail>> = _cDatails

    init {
        viewModelScope.launch(Dispatchers.Main) {
            val unCategory = repository.getUnCategory()
            if (unCategory.categoryId != -1) {
                repository.insertCategory(
                    CategoryEntity(
                        categoryId = TodoCategoryEnum.UNCATEGORY.code,
                        label = TodoCategoryEnum.UNCATEGORY.content
                    )
                )
            }
            queryCategoryDetails()
        }
    }

    suspend fun queryCategoryDetails() {
        val res = ArrayList<CategoryDetail>()
        val allCnt = repository.getAllTodoCnt() ?: 0
        val list: List<CategoryDetail> = repository.queryDetails()
//        val unCateCnt = repository.getUnCateTodoCnt() ?: 0

        res.apply {
            add(
                CategoryDetail(
                    TodoCategoryEnum.CATEGORY_ALL.code,
                    TodoCategoryEnum.CATEGORY_ALL.content,
                    allCnt
                )
            )
            addAll(list)
//            add(
//                CategoryDetail(
//                    TodoCategoryEnum.UNCATEGORY.code, TodoCategoryEnum.UNCATEGORY.content, unCateCnt
//                )
//            )
        }
        _cDatails.value = res
        Log.d(LoggerTag.MAIN_CATEGORY, "queryCategoryDetails: 刷新一下")
    }

    fun insertCategory(categoryEntity: CategoryEntity) {
        viewModelScope.launch {
            repository.insertCategory(categoryEntity)
            Log.d(LoggerTag.MAIN_CATEGORY, "insertCategory: 加加加")
            queryCategoryDetails()
        }
    }

    /**
     * 初始化删除模式下的选中项
     */
    fun initIsSelected() {
        val list = _cDatails.value?.toMutableList() ?: mutableListOf()
        list.map { it.isSelected = false }
    }

    fun deleteCategories(delList: List<CategoryEntity>) {
        viewModelScope.launch {
            repository.deleteCategories(*delList.toTypedArray())
            queryCategoryDetails()
        }
    }

}